﻿#r @"..\Microsoft.SharePoint.Client (spo)\Microsoft.SharePoint.Client.dll"
#r @"..\Microsoft.SharePoint.Client (spo)\Microsoft.SharePoint.Client.Runtime.dll"

#load "Client.fs"

open Client
open Microsoft.SharePoint.Client
open Microsoft.SharePoint.Client.Utilities
open Microsoft.SharePoint.Client.Sharing
open Microsoft.SharePoint.Client.Application
open System.Security;

// =======================================================
// Modify managed metadata navidation
// =======================================================

let web = Client.Context.Web;
Client.Load web

let list = web.Lists.GetByTitle("Documents")
Client.Load list

let folder = list.RootFolder
Client.Load folder

Client.Load folder.Properties

for prop in folder.Properties.FieldValues do
    let msg = prop.Key.ToString() + " | " + prop.Value.ToString()
    printfn "%s\n" msg

folder.Properties.["client_MOSS_MetadataNavigationSettings"]

let fld = list.Fields.GetByInternalNameOrTitle("Keywords")
Client.Load fld

fld.Id

let navXML = 
    @"<MetadataNavigationSettings 
        SchemaVersion=""1""
        IsEnabled=""True""
        AutoIndex=""True"">
        <NavigationHierarchies>
            <FolderHierarchy HideFoldersNode=""True"" />
        </NavigationHierarchies>
        <KeyFilters>
            <MetadataField FieldID=""" + fld.Id.ToString() + @""" FieldType=""TaxonomyFieldType"" CachedName=""DMSKeywords"" CachedDisplayName=""Keywords"" />
        </KeyFilters>
        <ManagedIndices>
            <ManagedIndex IndexID=""" + fld.Id.ToString() + @""" IndexFieldName=""DMSKeywords"" IndexFieldID=""" + fld.Id.ToString() + @""" />
        </ManagedIndices>
    </MetadataNavigationSettings>"

folder.Properties.["client_MOSS_MetadataNavigationSettings"] <- navXML

folder.Update()
list.Update()
Client.ExecuteQuery()
